package game;

import javax.accessibility.AccessibleContext;
import javax.swing.*;
import javax.tools.Diagnostic;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

/**
 * Date: 2023-04-25
 * Time: 14:02
 */
public class LoginJFrame extends JFrame implements ActionListener, MouseListener {

    static ArrayList<User> list = new ArrayList<>();
    static {
        list.add(new User("zhangsan","123"));
        list.add(new User("lisi","1234"));
    }

    // 创建登录的按钮
    JButton login = new JButton("登录");

    // 创建注册的按钮
    JButton register = new JButton("注册");

    JTextField userName = new JTextField();

    JTextField password = new JTextField();

    JTextField rightcode = new JTextField();

    String codeStr = CodeUtil.getCode();



    public LoginJFrame(){
        initJFrame();

        initView();
    }

    public void initJFrame() {
        this.setSize(488, 430);//设置宽高
        this.setTitle("拼图游戏 V1.0登录");//设置标题
        this.setDefaultCloseOperation(3);//设置关闭模式
        this.setLocationRelativeTo(null);//居中
        this.setAlwaysOnTop(true);//置顶
        this.setLayout(null);//取消内部默认布局
    }

    private void initView() {
/*        this.setSize(488,430);

        this.setTitle("登录");

        this.setAlwaysOnTop(true);

        this.setLocationRelativeTo(null);

        this.setDefaultCloseOperation(3);

        this.setLayout(null);*/



        ImageIcon userNameIcon = new ImageIcon("image/login/用户名.png");

        JLabel userJLabel = new JLabel(userNameIcon);
        userJLabel.setBounds(116,135,47,17);
        this.getContentPane().add(userJLabel);

        userName.setBounds(195,134,200,35);
        userName.setVisible(true);
        this.getContentPane().add(userName);

/*        JLabel jLabel2 = new JLabel(new ImageIcon("image/login/对话框.png"));
        jLabel2.setBounds(200,140,198,40);
        jLabel2.setVisible(true);
        this.getContentPane().add(jLabel2);*/
        ImageIcon passwordIcon = new ImageIcon("image/login/密码.png");
        
        JLabel userJLabel2 = new JLabel(passwordIcon);
        userJLabel2.setBounds(130,195,32,16);
        this.getContentPane().add(userJLabel2);

        password.setBounds(195,195,200,30);
        password.setVisible(true);
        this.getContentPane().add(password);

/*        ImageIcon bg1 = new ImageIcon("image/login/对话框.png");
        JLabel jLabel1 = new JLabel();
        jLabel1.setBounds(210,200,198,40);
        jLabel1.setVisible(true);
        this.getContentPane().add(jLabel1);*/


        ImageIcon icon = new ImageIcon("image/login/验证码.png");
        
        JLabel userJLabel3 = new JLabel(icon);
        userJLabel3.setBounds(133,256,50,30);
        this.getContentPane().add(userJLabel3);


        rightcode.setBounds(195,256,100,30);
        rightcode.setVisible(true);
        this.getContentPane().add(rightcode);

/*        ImageIcon bg3 = new ImageIcon("image/login/对话框.png");
        JLabel jLabel3 = new JLabel();
        jLabel3.setBounds(210,250,198,40);
        jLabel3.setVisible(true);
        this.getContentPane().add(jLabel3);*/


        JLabel jLabel = new JLabel(codeStr);
        jLabel.setBounds(300,256,50,30);
        this.getContentPane().add(jLabel);


        login.setBounds(123,310,128,47);
        login.setIcon(new ImageIcon("image/login/登录按钮.png"));
        // 去除边框
        login.setBorderPainted(false);
        // 去处按钮的背景
        login.setContentAreaFilled(false);
        this.getContentPane().add(login);


        register.setBounds(256,310,128,47);
        register.setIcon(new ImageIcon("image/register/注册按钮.png"));
        // 去除边框
        register.setBorderPainted(false);
        // 去处按钮的背景
        register.setContentAreaFilled(false);
        this.getContentPane().add(register);


        login.addActionListener(this);
        register.addActionListener(this);
        login.addMouseListener(this);
        register.addMouseListener(this);


        ImageIcon bg = new ImageIcon("image/login/background.png");
        JLabel label = new JLabel(bg);
        label.setBounds(0,0,470,390);
        this.getContentPane().add(label);

        this.setVisible(true);
    }

    //要展示用户名或密码错误
    public void showJDialog(String content) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200, 150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);

        //创建Jlabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }



    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source == login){
            System.out.println("登录");
            login.setIcon(new ImageIcon("image/login/登录按下.png"));

        } else if (source == register) {
            System.out.println("注册");
            register.setIcon(new ImageIcon("image/login/注册按下2.png"));
        }

    }

    @Override
    public void mouseClicked(MouseEvent mouseEvent) {
        Object source = mouseEvent.getSource();

        
        if(source == login){
             if(!userName.getText().equals("zhangsan")){
                 showJDialog("用户名或密码错误");
                 return;
             }
             if(!password.getText().equals("123")){
                 showJDialog("用户名或密码错误");
                 return;
             }
             if(!rightcode.getText().equals(codeStr)){
                 showJDialog("验证码错误");
             }
        }
        // 用户名和密码正确,隐藏登录界面,展示游戏界面
        this.setVisible(false);
        new GameJFrame();
    }

    @Override
    public void mousePressed(MouseEvent mouseEvent) {
        Object source = mouseEvent.getSource();
        if(source == login){
            login.setIcon(new ImageIcon("image/register/重置按钮.png"));
        }
        //this.getContentPane().repaint();
    }

    @Override
    public void mouseReleased(MouseEvent mouseEvent) {
        Object source = mouseEvent.getSource();
        if(source == login){
            login.setIcon(new ImageIcon("image/login/登录按下.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent mouseEvent) {

    }

    @Override
    public void mouseExited(MouseEvent mouseEvent) {

    }
}

